package com.nclg.exam.controller;

import com.github.pagehelper.PageInfo;
import com.nclg.exam.dto.UpdateUserDto;
import com.nclg.exam.dto.UserDto;
import com.nclg.exam.dto.UserPageDto;
import com.nclg.exam.entity.LayResponse;
import com.nclg.exam.entity.TUser;
import com.nclg.exam.service.AdminService;
import com.nclg.exam.service.UserService;
import com.nclg.exam.utils.JsonResult;
import com.nclg.exam.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Map;

@RestController
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private UserService userService;

    @Autowired
    private AdminService adminService;

    /**
     * 分页查询数据
     * @param dto
     * @return
     */
    @GetMapping("/selectByPage")
    public JsonResult selectByPage(UserDto dto){
        PageInfo<UserPageDto> users = adminService.selectByPage(dto);
        LayResponse<UserPageDto> layResponse = new LayResponse<>("0", "", users.getTotal(), users.getList());
        return JsonResult.success(layResponse);
    }

    /**
     * 修改用户状态
     * @param uid
     * @return
     */
    @PutMapping("/updateStatus/{uid}/{ustatus}")
    public JsonResult updateStatus(@PathVariable String uid, @PathVariable String ustatus){
        boolean isTrue = adminService.updateStatus(Long.valueOf(uid), ustatus);
        if (!isTrue) return JsonResult.failure("禁用/启用失败");
        return JsonResult.success("禁用/启用成功");
    }

    /**
     * 重置密码
     * @param uid
     * @return
     */
    @PutMapping("/resetPwd/{uid}")
    public JsonResult resetPwd(@PathVariable String uid){
        Long uId = Long.valueOf(uid);
        TUser user = userService.selectByPrimaryKey(uId);
        if (null == user) return JsonResult.failure("用户不存在");
        String resetPwd = "123456";
        String pwd = MD5Utils.encrypt(resetPwd, user.getUSalt());
        boolean isTrue = userService.updatePwd(uId,pwd);
        if (!isTrue) return JsonResult.failure("密码重置失败");
        return JsonResult.success("密码重置成功,密码为：" + resetPwd);
    }

    /**
     * 添加用户
     * @param tUser
     * @return
     */
    @PostMapping("/addUser")
    public JsonResult addUser(TUser tUser){
        String initPwd = "123456";
        Map<String, String> map = MD5Utils.md5(initPwd);
        tUser.setUPassword(map.get("password"));
        tUser.setUSalt(map.get("salt"));
        tUser.setUIco("/static/images/ico/头像2.png");
        tUser.setUStatus("1");
        if (!userService.insertSelective(tUser)) return JsonResult.failure("添加失败");
        return JsonResult.success("用户 " + tUser.getUName() + " 添加成功，初始密码为：" + initPwd);
    }

    /**
     * 修改用户权限
     * @param uid
     * @param urole
     * @return
     */
    @PutMapping("/updateRole/{uid}/{urole}")
    public JsonResult updateRole(@PathVariable String uid, @PathVariable String urole){
        boolean isTrue = adminService.updateRole(Long.valueOf(uid), urole);
        if (!isTrue) return JsonResult.failure("权限修改失败");
        return JsonResult.success("权限修改成功：" + (urole.equals("0") ? "学生" : "管理员"));
    }

    /**
     * 修改个人信息
     * @param dto
     * @return
     */
    @PutMapping("/updateOther")
    public JsonResult update(@Valid UpdateUserDto dto, HttpServletRequest request){
        boolean isTrue = userService.updateByPrimaryKeySelective(dto);
        if (!isTrue) return JsonResult.failure("修改失败");
        return JsonResult.success("修改成功");
    }
}
